//
//  TJUserAcount.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/10.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class TJUserAcount: NSObject , NSCoding {

    // 获取授权后的令牌.
    var access_token:String?
    // access_token的声生命周期.  加个属性监控器,一但expires_in有值,立马给dueDate赋值.
    var expires_in:NSTimeInterval = 0 {
    
        didSet {
        
            dueDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    // 到期时间.
    var dueDate:NSDate?
    
    // 当前授权用户的uid
    var uid:String?
    
    // 用户昵称.
    var screen_name:String?
    
    // 用户头像.
    var avatar_large:String?
    
    
    // 构造器.
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    // 重写undefineKye.
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
//        TJPrint("UndefinedKey = \(key)")
    }
    
    // 实现对象的打印信息:只读的计算属性.
    override var description:String {
    
        get {
        
            return ("access_token=\(access_token), expires_in=\(expires_in), dueDate=\(dueDate), uid=\(uid) ,screen_name\(screen_name) ,avatar_large\(avatar_large)")
        }
    }
    
    // MARK  解挡属性.
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        dueDate = aDecoder.decodeObjectForKey("dueDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    
    // MARK - 归档属性
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(dueDate, forKey: "dueDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
}
